// 用户词库 存在 MySQL里面 工厂单例
#ifndef USERDICT_H
#define USERDICT_H

#include "pinyinhandler.h"
#include "encryptutil.h"
#include "IUserDict.h"  // 包含接口头文件
#include "OpenCCUtil.h"

#include <QObject>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QVector>
#include <QMutex>

// 前置声明
struct PinyinCandidate;
class PinyinGeneratorInterface;
class PinyinHandler;

// 只虚继承IUserDict（IUserDict已虚继承QObject），避免QObject多继承
class UserDict : public virtual IUserDict
{
    Q_OBJECT
    Q_DISABLE_COPY_MOVE(UserDict)  // 禁止拷贝移动

public:
    // 单例 
    static UserDict* getInstance(QObject *parent = nullptr);
    ~UserDict() override;

    // 数据库相关
    bool initDatabase() override;                               
    bool isConnected() const override { return m_isConnected; }  

    // 用户词CRUD
    bool addWord(const QString &word, const QString &pinyin = "", int frequency = 1) override;
    bool removeWord(const QString &word) override;
    bool updateFrequency(const QString &word, int increment = 1) override;
    QVector<PinyinCandidate> queryWords(const QString &pinyin) override;        
    bool exportDict(const QString &exportPath, bool encrypt = true) override;    
    bool importDict(const QString &importPath) override;
    int getWordCount() const override;
    bool wordExists(const QString &word) override;

    // 依赖注入
    void setPinyinGenerator(PinyinGeneratorInterface* generator) override;

    // 简繁同步
    QString generateTraditionalWord(const QString& simplifiedWord);
    bool syncSimplifiedTraditional(const QString& simplifiedWord, QString& traditionalWord);

signals:
    void dictChanged() override;
    void databaseError(const QString &errorMsg) override;
    void wordSynced(const QString& simplified, const QString& traditional);

private:
    // 隐藏构造
    explicit UserDict(QObject *parent = nullptr);

    // 内部函数
    bool createTables();
    QString generatePinyin(const QString &word);
    QByteArray encryptField(const QString& text) const;             
    QString decryptField(const QByteArray& encryptedData) const;    
    QString getDeviceUniqueId() const;                              
    QString escapeCsvField(const QString& field) const;             

private:
    static UserDict* m_instance;
    static QMutex m_instanceMutex;
    static const QByteArray FIXED_IV;

    QSqlDatabase m_db;
    bool m_isConnected;
    QString m_mySQLTable = "user_words";

    QByteArray m_encryptKey;
    bool m_encryptEnabled = true;
    
    PinyinGeneratorInterface* m_pinyinGenerator = nullptr;
    mutable QMutex m_dbMutex;
    OpenCCUtil* m_openccUtil = nullptr;

};

// 拼音生成接口
class PinyinGeneratorInterface
{
public:
    virtual ~PinyinGeneratorInterface() = default;
    virtual QString generatePinyin(const QString& word) const = 0;
};

// PinyinHandler适配器
class PinyinHandlerAdapter : public PinyinGeneratorInterface
{
public:
    explicit PinyinHandlerAdapter(PinyinHandler* handler) : m_handler(handler) {}
    QString generatePinyin(const QString& word) const override {
        if (!m_handler) return "";
        return m_handler->convertTextToPinyin(word);
    }

private:
    PinyinHandler* m_handler = nullptr;

};

#endif	// USERDICT_H